//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        isClick:0,
        addressList:[],
        returnPage:''
    },
    onLoad:function(options){
        if(options.returnPage){
            this.setData({
                returnPage:options.returnPage
            });
        }
        this.getAddresList()

    },
    getAddresList:function () {
        var that = this;
        wx.showToast({
            icon:'loading',
            title:'加载中'
        })
        app.util.request({
            url:app.globalRequestUrl + '/addrlist',
            method:'GET',
            success:function (res) {
                if(res && res.msg == 'success'){
                    that.setData({
                       addressList:res.data
                   })
                }
            },
            complete:()=>  {
                wx.hideToast()
                // console.log(this);
            }
        })
    },
    bindInputContent:function (e) {
        this.data.content = e.detail.value;
    },
    addAddress:function () {
        wx.navigateTo({
            url:'/pages/address/address'
        })
    },
    bindAddrUse:function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var addreList = that.data.addressList;
        var addrData = {};
        for (var i = 0;i < addreList.length;i++){
            if(addreList[i].a_id == id){
                addrData = addreList[i];
                break;
            }
        }
        wx.setStorageSync('curr_address',JSON.stringify(addrData));
        wx.navigateBack();
    },
    bindAddrDel:function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        wx.showModal({
            title:"提示",
            content:'确认删除该地址吗?',
            success:function (res) {
                if (res.confirm) {
                    that.addDel(id)
                }
            }
        })
    },
    addDel:function (id) {
        var that = this;
        wx.showToast({
            icon:'loading',
            title:'加载中'
        })
        app.util.request({
            url:app.globalRequestUrl + '/deladdr',
            data:{a_id:id},
            method:'GET',
            success:function (res) {
                if(res && res.msg == 'success'){
                    that.getAddresList();
                }
            },
            complete:()=>  {
                wx.hideToast()
                // console.log(this);
            }
        })
    }
})
